"use client";
import { Show } from "@refinedev/antd";
import { useShow } from "@refinedev/core";
import { Descriptions, Tag } from "antd";
import { useEffect, useState } from "react";
import dayjs from "dayjs";
import { getLogininforDetail } from "@common/api/logininfor_action";

export default function LogininforShow({ params }: { params: { id: string } }) {
  const [data, setData] = useState<any>(null);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    const fetchData = async () => {
      try {
        setLoading(true);
        const result: any = await getLogininforDetail(Number(params.id));
        if (result.code === 200) {
          setData(result.data);
        }
      } catch (error) {
        console.error('获取登录信息详情失败:', error);
      } finally {
        setLoading(false);
      }
    };

    if (params.id) {
      fetchData();
    }
  }, [params.id]);

  const getStatusText = (status: string) => {
    return status === '0' ? '成功' : '失败';
  };

  const getStatusColor = (status: string) => {
    return status === '0' ? 'green' : 'red';
  };

  return (
    <Show>
      {loading ? (
        <div>加载中...</div>
      ) : data ? (
        <Descriptions bordered column={2}>
          <Descriptions.Item label="访问ID" span={1}>
            {data.info_id}
          </Descriptions.Item>
          <Descriptions.Item label="用户名" span={1}>
            {data.user_name}
          </Descriptions.Item>
          <Descriptions.Item label="IP地址" span={1}>
            {data.ipaddr}
          </Descriptions.Item>
          <Descriptions.Item label="登录地点" span={1}>
            {data.login_location || '-'}
          </Descriptions.Item>
          <Descriptions.Item label="浏览器" span={1}>
            {data.browser || '-'}
          </Descriptions.Item>
          <Descriptions.Item label="操作系统" span={1}>
            {data.os || '-'}
          </Descriptions.Item>
          <Descriptions.Item label="登录状态" span={1}>
            <Tag color={getStatusColor(data.status)}>
              {getStatusText(data.status)}
            </Tag>
          </Descriptions.Item>
          <Descriptions.Item label="登录时间" span={1}>
            {data.login_time ? dayjs(data.login_time).format('YYYY-MM-DD HH:mm:ss') : '-'}
          </Descriptions.Item>
          <Descriptions.Item label="提示消息" span={2}>
            {data.msg || '-'}
          </Descriptions.Item>
        </Descriptions>
      ) : (
        <div>未找到数据</div>
      )}
    </Show>
  );
} 